
package com.sun.showcase.biz.mall.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.exception.BusinessException;
import com.sun.showcase.biz.mall.dao.MemberCashApplyDao;
import com.sun.showcase.biz.mall.service.MemberBillService;
import com.sun.showcase.biz.mall.service.MemberCashApplyService;
import com.sun.showcase.biz.mall.service.MemberService;
import com.sun.showcase.client.domain.mall.Member;
import com.sun.showcase.client.domain.mall.MemberCashApply;
import com.sun.showcase.client.query.mall.MemberBillQuery;
import com.sun.showcase.client.query.mall.MemberCashApplyQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.ExecuteResult;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
import com.sun.showcase.utils.ValidatorUtils;
@Service("memberCashApplyService")
@Transactional
public class MemberCashApplyServiceImpl implements MemberCashApplyService{
	@Autowired
	private MemberCashApplyDao memberCashApplyDao;
	@Autowired
	private MemberService memberService;
	@Autowired
	private MemberBillService memberBillService;
	
	public void setMemberCashApplyDao(MemberCashApplyDao dao) {
		this.memberCashApplyDao = dao;
	}

	public DataGrid datagrid(MemberCashApplyQuery memberCashApplyQuery) {
		DataGrid j = new DataGrid();
		Pager<MemberCashApply> pager  = find(memberCashApplyQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<MemberCashApplyQuery> getQuerysFromEntitys(List<MemberCashApply> memberCashApplys) {
		List<MemberCashApplyQuery> memberCashApplyQuerys = new ArrayList<MemberCashApplyQuery>();
		if (memberCashApplys != null && memberCashApplys.size() > 0) {
			for (MemberCashApply tb : memberCashApplys) {
				MemberCashApplyQuery b = new MemberCashApplyQuery();
				BeanUtils.copyProperties(tb, b);
				memberCashApplyQuerys.add(b);
			}
		}
		return memberCashApplyQuerys;
	}

	private Pager<MemberCashApply> find(MemberCashApplyQuery memberCashApplyQuery) {
		Page<MemberCashApply> page = PageHelper.startPage(memberCashApplyQuery.getPage().intValue(), memberCashApplyQuery.getRows().intValue());
		List<MemberCashApply> list = memberCashApplyDao.findList(memberCashApplyQuery);
		Pager<MemberCashApply> pager = new Pager<MemberCashApply>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public MemberCashApply add(MemberCashApplyQuery memberCashApplyQuery) {
		MemberCashApply t = new MemberCashApply();
		BeanUtils.copyProperties(memberCashApplyQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		memberCashApplyDao.save(t);
		memberCashApplyQuery.setId(t.getId());
		return t;
		
	}

	public void update(MemberCashApplyQuery memberCashApplyQuery) {
		MemberCashApply t = memberCashApplyDao.getById(memberCashApplyQuery.getId());
	    if(t != null) {
	    	if("0".equals(t.getStatus()) && "1".equals(memberCashApplyQuery.getStatus())) {
	    		//审核通过
	    		//memberService.updateBalance(t.getUserId(), t.getAmount()*-1,0D);
	    	}else if("1".equals(t.getStatus()) && "3".equals(memberCashApplyQuery.getStatus())) {
	    		//确认转账，修改账单状态
	    		memberBillService.updateStatus(t.getId(), "1");
	    	}else if("0".equals(t.getStatus()) && "2".equals(memberCashApplyQuery.getStatus())) {
	    		//不通过，返还余额
	    		memberService.updateBalance(t.getUserId(), t.getAmount(),0D);
	    		//更新账单状态，已取消
	    		memberBillService.updateStatus(t.getId(), "2");
	    	}
	    	BeanUtils.copyProperties(memberCashApplyQuery, t);
	    	t.setOperaterId(LoginContextHolder.get().getUserId());
	  	    t.setOperaterName(LoginContextHolder.get().getUserName());
	    	memberCashApplyDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(MemberCashApplyQuery memberCashApplyQuery) {
	    memberCashApplyDao.updatePart(memberCashApplyQuery);
	}

	public void delete(java.lang.String[] ids) {
		memberCashApplyDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.String[] ids) {
		memberCashApplyDao.deletePt(ids);
	}

	public MemberCashApply get(MemberCashApplyQuery memberCashApplyQuery) {
		return memberCashApplyDao.getById(memberCashApplyQuery.getId());
	}

	public MemberCashApply get(String id) {
		return memberCashApplyDao.getById(id);
	}

	
	public List<MemberCashApplyQuery> listAll(MemberCashApplyQuery memberCashApplyQuery) {
	    List<MemberCashApply> list = memberCashApplyDao.findList(memberCashApplyQuery);
		List<MemberCashApplyQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public ExecuteResult<MemberCashApply> save(MemberCashApplyQuery memberCashApplyQuery) {
		ValidatorUtils.validate(memberCashApplyQuery);
		ExecuteResult<MemberCashApply> res = new ExecuteResult<MemberCashApply>();
		Member mem = memberService.get(memberCashApplyQuery.getUserId());
		if(mem == null) {
			res.addErrorMessage("无效会员");
			return res;
		}
		if(mem.getBalance()<memberCashApplyQuery.getAmount()) {
			res.addErrorMessage("无效提现金额");
			return res;
		}
		memberCashApplyQuery.setUserName(mem.getNickName());
		memberCashApplyQuery.setStatus(StringConstant.ZERO);
		this.add(memberCashApplyQuery);
		//形成账单
		MemberBillQuery ad = new MemberBillQuery();
		ad.setBussinessId(memberCashApplyQuery.getId());
		ad.setAmount(memberCashApplyQuery.getAmount());
		//ad.setServiceFee(0D);
		//ad.setActualAmount(ArithUtil.sub(ad.getAmount(), ad.getServiceFee()));
		ad.setType("1");
		ad.setStatus("0");
		ad.setUserId(memberCashApplyQuery.getUserId());
		ad.setUserName(memberCashApplyQuery.getUserName());
		ad.setRemark("提现");
		memberBillService.add(ad);
		//扣除余额
		Long l = memberService.updateBalance(memberCashApplyQuery.getUserId(), memberCashApplyQuery.getAmount()*-1, 0D);
		if(l != 1) {
			throw new BusinessException("申请失败，请重试");
		}
		return res;
	}
	
	
}
